package com.toefl.speaking.business;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.toefl.speaking.dao.SQLiteHelper;
import com.toefl.speaking.dao.bean.QuestionBean;
import com.toefl.speaking.exception.Logger;
import com.toefl.speaking.schema.SchemaFieldConstants.QuestionFields;
import com.toefl.speaking.util.StringUtils;

public class QuestionBusiness {

	private static final String TAG = "QuestionBusiness";

	// select * from policy_customer where cntrct_no=? and is_owner =?
	private static final String SQL_QUESTIONINFO_BY_ID = "select * from " + QuestionFields.TABLE_NAME + " where "
			+ QuestionFields.QUESTION_ID + "=?";

	/**
	 * 
	 * @param question_id
	 * @return
	 */
	public static QuestionBean getQuestionInfoByQID(final String question_id) {
		if (StringUtils.isEmpty(question_id)) {
			return null;
		}
		QuestionBean question = null;
		SQLiteDatabase db = null;
		Cursor cursor = null;
		try {
			db = SQLiteHelper.getReadableDatabase();
			if (db != null) {
				cursor = db.rawQuery(SQL_QUESTIONINFO_BY_ID, new String[] { question_id });
				if (cursor != null && cursor.moveToNext()) {
					question = populateQuestionInfo(cursor);
				}
			}
		} finally {
			SQLiteHelper.closeDb(db, cursor);
		}
		return question;
	}

	public static boolean isExistQuestionBean(final String question_id) {
		QuestionBean question = getQuestionInfoByQID(question_id);
		if (question != null) {
			return true;
		}
		return false;
	}

	public static void removeQuestion(final String questionId) {
		if (!StringUtils.isEmpty(questionId)) {
			SQLiteDatabase db = null;
			try {
				db = SQLiteHelper.getWritableDatabase();
				if (db != null) {
					db.execSQL("delete from " + QuestionFields.TABLE_NAME + " where " + QuestionFields.QUESTION_ID
							+ "=? ", new String[] { questionId });
				}
			} finally {
				SQLiteHelper.closeDb(db, null);
			}
		}
	}

	public static boolean saveUpdateCustomer(QuestionBean qustion) {
		SQLiteDatabase db = null;
		long recordId = 0;
		try {
			db = SQLiteHelper.getWritableDatabase();
			if (db != null) {
				ContentValues values = new ContentValues();
				values.put(QuestionFields.QUESTION_ID, qustion.question_id);
				values.put(QuestionFields.QUESTION_CONTENT, qustion.question_content);
				values.put(QuestionFields.QUESTION_TITLE, qustion.question_title);
				values.put(QuestionFields.QUESTION_TYPE, qustion.question_type);
				values.put(QuestionFields.QUESTION_LISTENINT_PATH, qustion.question_listent_path);
				recordId = 0;
				recordId = db.insert(QuestionFields.TABLE_NAME, "", values);
			}
		} catch (Exception ex) {
			Logger.printStackTrace(TAG, ex);
		} finally {
			SQLiteHelper.closeDb(db, null);
		}
		return recordId > 0L;
	}

	public static boolean updateCustomer(QuestionBean qustion) {
		SQLiteDatabase db = null;
		long recordId = 0;
		try {
			db = SQLiteHelper.getWritableDatabase();
			if (db != null) {
				ContentValues values = new ContentValues();
				// values.put(QuestionFields.QUESTION_ID, qustion.question_id);
				if (qustion.question_content != null) {
					values.put(QuestionFields.QUESTION_CONTENT, qustion.question_content);
				}
				if (qustion.question_title != null) {
					values.put(QuestionFields.QUESTION_TITLE, qustion.question_title);
				}
				if (qustion.question_type != 0) {
					values.put(QuestionFields.QUESTION_TYPE, qustion.question_type);
				}
				if (qustion.question_listent_path != null) {
					values.put(QuestionFields.QUESTION_LISTENINT_PATH, qustion.question_listent_path);
				}
				recordId = 0;
				recordId = db.update(QuestionFields.TABLE_NAME, values, QuestionFields.QUESTION_ID + "=? ",
						new String[] { String.valueOf(qustion.question_id) });
			}
		} catch (Exception ex) {
			Logger.printStackTrace(TAG, ex);
		} finally {
			SQLiteHelper.closeDb(db, null);
		}
		return recordId > 0L;
	}

	private static QuestionBean populateQuestionInfo(Cursor cursor) {
		QuestionBean question = new QuestionBean();
		question.question_id = cursor.getInt(cursor.getColumnIndex(QuestionFields.QUESTION_ID));
		question.question_content = cursor.getString(cursor.getColumnIndex(QuestionFields.QUESTION_CONTENT));
		question.question_title = cursor.getString(cursor.getColumnIndex(QuestionFields.QUESTION_TITLE));
		question.question_type = cursor.getInt(cursor.getColumnIndex(QuestionFields.QUESTION_TYPE));
		question.question_listent_path = cursor
				.getString(cursor.getColumnIndex(QuestionFields.QUESTION_LISTENINT_PATH));
		return question;
	}

}
